package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Consumption;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.enums.ConsumptionStatusEnum;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionItemService;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.qo.QueryObject;

import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.JsonResult;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {

    @Autowired
    private IConsumptionService consumptionService;

    @RequiredPermission(name="结算单主页面",expression="consumption:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<Consumption> pageInfo = consumptionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "consumption/list";
    }

    @RequestMapping("/delete")
    @RequiredPermission(name="结算单删除",expression="consumption:delete")
    @ResponseBody
    public JsonResult delete(Long id){
        if (id != null) {
            consumptionService.delete(id);
        }
        return new JsonResult();
    }

    //无预约单新增
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission(name="结算单新增/编辑",expression="consumption:saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Consumption consum){
        if (consum.getId() != null) {
            consumptionService.update(consum);
        }else {
            //consumptionService.save(consum);
        }
        return new JsonResult();
    }

    /**
     * 有预约新增和无预约新增处理不同，分开写比较好
     * @param
     * @return
     */
    //有预约新增
    @RequestMapping("/save")
    @RequiredPermission(name="结算单新增(关联预约单)",expression="consumption:save")
    @ResponseBody
    public JsonResult save(String appointmentAno){
        Consumption consumption = consumptionService.save(appointmentAno);
        JsonResult jsonResult =  new JsonResult();
        jsonResult.setData(consumption.getId());//当前结算单的id
        return jsonResult;
    }

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private IConsumptionItemService consumptionItemService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @RequestMapping("/input")
    @RequiredPermission(name="结算单新增",expression="consumption:input")
    public String input(Model model,Long id,String cno){
        //回显消费门店
        model.addAttribute("businesses",businessService.selectAll());
        //若传过来的id不为空,将用户的信息共享到结算单信息页面
        if(id!=null){
            model.addAttribute("consumption",consumptionService.get(id));
        }
        //结算单明细中的数据
        model.addAttribute("consumptionItems",consumptionItemService.selectByCno(cno));

        //结算单明细模态框中业务大类下拉框的数据
        model.addAttribute("systemDictionaryItems",systemDictionaryItemService.selectAll());

        return "/consumption/input";
    }
}
